﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;


namespace HRMS.Controllers
{
    public class DepartmentsModel
    {

        public static bool AddDepartment(Department department)
        {
            try
            {
                using (var deptEntity = new HRMSEntities())
                {
                    deptEntity.Departments.AddObject(department);
                    deptEntity.SaveChanges();
                    return true;
                }

            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteDepartment(int id)
        {
            try
            {
                using (var deptEntity = new HRMSEntities())
                {
                    deptEntity.Departments.DeleteObject(deptEntity.Departments.First(d => d.DepartmentId == id));
                    deptEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<Department> GetDepartments(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        
        {
            using (var depEmtity = new HRMSEntities())
            {
                return depEmtity.Departments.OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }

        public static int GetDepartmentsCount()
        {
            using (var deptEntity = new HRMSEntities())
            {
                return deptEntity.Departments.Count();
            }
        }

        public static bool UpdateDepartment(Department department)
        {
            try
            {
                using (var deptEntity = new HRMSEntities())
                {
                    var departmentToEdit = deptEntity.Departments.FirstOrDefault(d => d.DepartmentId == department.DepartmentId);
                    if (departmentToEdit != null) departmentToEdit.DepartmentName = department.DepartmentName;
                    deptEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<string> DepartmentsList()
        {
            var departmentList = new List<string>();
            try
            {
                using (var deptEntity = new HRMSEntities())
                {
                    var departments = deptEntity.Departments.ToList();
                    departmentList.AddRange(departments.Select(department => department.DepartmentName));
                }
                return departmentList;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return departmentList;
            }
        }
    }
}